home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1988-02-13 | 9.3 KB | 319 lines |
- IMPLEMENTATION MODULE Text;
-
-
- (********************************************************************)
-
- PROCEDURE Length ( VAR Source : (* IN *) ARRAY OF CHAR ) : CARDINAL;
-
- VAR Count : CARDINAL;
-
- BEGIN
- Count := 0;
-
- (*--- Loop until found a null character or exceeded the ---*)
- (*--- maximum physical length of the string. ---*)
-
- WHILE (Count <= HIGH (Source)) AND (Source[Count] <> CHR (0)) DO
- INC (Count);
- END;
- RETURN ( Count );
- END Length;
-
- (********************************************************************)
-
- PROCEDURE Assign (
- VAR Source : (* IN *) ARRAY OF CHAR;
- VAR Destination : (* OUT *) ARRAY OF CHAR );
-
- VAR Index : CARDINAL;
-
- BEGIN
- Index := 0;
-
- (*--- Copy the characters from Source to Destination ---*)
- (*--- one at a time. ---*)
-
- LOOP
- IF Index = HIGH (Destination) THEN
-
- (*--- There is no more space left in Destination, so ---*)
- (*--- finish the destination by adding a null character. ---*)
-
- Destination[Index] := CHR (0);
- EXIT;
-
- ELSIF Index > HIGH (Source) THEN
-
- (*--- We have copied all of the characters from Source ---*)
- (*--- into Destination, so finish the destination by ---*)
- (*--- adding a null character. ---*)
-
- Destination[Index] := CHR (0);
- EXIT;
-
- ELSIF Source[Index] = CHR (0) THEN
-
- (*--- We have encountered the logical end of the source. ---*)
- (*--- so finish the destination by adding a null ---*)
- (*--- character. ---*)
-
- Destination[Index] := CHR (0);
- EXIT;
-
- ELSE
-
- (*--- Continue to copy a character from Source to ---*)
- (*--- Destination. ---*)
-
- Destination[Index] := Source[Index];
- INC (Index);
- END;
- END;
- END Assign;
-
- (********************************************************************)
-
- PROCEDURE Compare (
- VAR LeftString : (* IN *) ARRAY OF CHAR;
- VAR RightString : (* IN *) ARRAY OF CHAR ) : CompareResult;
-
- VAR
- LeftLength : CARDINAL;
- RightLength : CARDINAL;
- Index : CARDINAL;
-
- BEGIN
-
- (*--- Get the lengths of both strings ---*)
-
- LeftLength := Length ( LeftString );
- RightLength := Length ( RightString );
-
- IF LeftLength = 0 THEN
- IF RightLength = 0 THEN
- RETURN (Equal);
- ELSIF RightLength > 0 THEN
- RETURN (LessThan);
- END;
-
- ELSE
-
- (*-- Iterate through the characters of LeftString, comparing --*)
- (*-- each character of LeftString with that of RightString. --*)
-
- FOR Index := 0 TO LeftLength - 1 DO
- IF Index >= RightLength THEN
-
- (*--- Up to this point, each character of LeftString --*)
- (*--- has matched that of RightString. Since we have --*)
- (*--- now exhausted RightString (LeftString is longer), --*)
- (*--- indicate that LeftString is greater than --*)
- (*--- RightString. --*)
-
- RETURN ( GreaterThan );
-
- ELSIF LeftString[Index] < RightString[Index] THEN
-
- (*--- The character from LeftString is less than the ---*)
- (*--- corresponding character from RightString. ---*)
-
- RETURN ( LessThan );
-
- ELSIF LeftString[Index] > RightString[Index] THEN
-
- (*-- The character from LeftString is greater than the ---*)
- (*-- corresponding character from RightString. ---*)
-
- RETURN ( GreaterThan );
- END;
- END;
- END;
-
- (*--- All of the characters from LeftString match that from ---*)
- (*--- RightString, so see if LeftString is shorter. ---*)
-
- IF LeftLength = RightLength THEN
-
- (*--- The lengths are equal, so the strings are equal. ---*)
-
- RETURN ( Equal );
-
- ELSE
-
- (*--- LeftString is shorter than RightString. ---*)
-
- RETURN ( LessThan );
- END;
- END Compare;
-
- (********************************************************************)
-
- PROCEDURE SliceChar (
- VAR Source : (* IN *) ARRAY OF CHAR;
- Position : (* IN *) CARDINAL;
- VAR Result : (* OUT *) CHAR ) : BOOLEAN;
-
- BEGIN
- IF Position >= Length ( Source ) THEN
-
- (*--- The requested position exceeds the current length ---*)
- (*--- of the string. ---*)
-
- RETURN ( FALSE );
- ELSE
- Result := Source[Position];
- RETURN ( TRUE );
- END;
- END SliceChar;
-
- (********************************************************************)
-
- PROCEDURE SliceString (
- VAR Source : (* IN *) ARRAY OF CHAR;
- Start : (* IN *) CARDINAL;
- Stop : (* IN *) CARDINAL;
- VAR Result : (* OUT *) ARRAY OF CHAR ) : BOOLEAN;
-
- VAR
- SourceIndex : CARDINAL;
- ResultIndex : CARDINAL;
-
- BEGIN
- IF Start > Stop THEN
-
- (*--- The Start index exceeds the Stop index. ---*)
-
- RETURN ( FALSE );
-
- ELSIF Stop >= Length ( Source ) THEN
-
- (*--- The Stop index is greater than the current length ---*)
- (*--- of the string. ---*)
-
- RETURN ( FALSE );
-
- ELSIF Stop - Start > HIGH (Result) THEN
-
- (*--- The slice result exceeds the storage available. ---*)
-
- RETURN ( FALSE );
-
- ELSE
-
- (*--- Copy the characters from the source string to ---*)
- (*--- Result, starting at the position indicated by ---*)
- (*--- Start. ---*)
-
- ResultIndex := 0;
- FOR SourceIndex := Start TO Stop DO
- Result[ResultIndex] := Source[SourceIndex];
- INC ( ResultIndex );
- END;
- IF ResultIndex <= HIGH (Result) THEN
-
- (*--- Result can contain more characters than was ---*)
- (*--- sliced out of the source string, so terminate ---*)
- (*--- Result with a null character. ---*)
-
- Result[ResultIndex] := CHR (0);
- END;
- RETURN ( TRUE );
- END;
- END SliceString;
-
- (********************************************************************)
-
- PROCEDURE ConcatChar (
- VAR Source : (* IN *) ARRAY OF CHAR;
- Character : (* IN *) CHAR;
- VAR Result : (* OUT *) ARRAY OF CHAR ) : BOOLEAN;
-
- VAR CharString : ARRAY [0..0] OF CHAR;
-
- BEGIN
-
- (*--- Make the character into a single character string, ---*)
- (*--- so we can save code by calling ConcatString. ---*)
-
- CharString[0] := Character;
- RETURN ( ConcatString ( Source, CharString, Result ) );
- END ConcatChar;
-
-
- (********************************************************************)
-
- PROCEDURE ConcatString (
- VAR LeftString : (* IN *) ARRAY OF CHAR;
- VAR RightString : (* IN *) ARRAY OF CHAR;
- VAR Result : (* OUT *) ARRAY OF CHAR ) : BOOLEAN;
-
- VAR
- LeftLength : CARDINAL;
- RightLength : CARDINAL;
- SourceIndex : CARDINAL;
- ResultIndex : CARDINAL;
-
- BEGIN
-
- (*--- Get the lengths of both strings. ---*)
-
- LeftLength := Length ( LeftString );
- RightLength := Length ( RightString );
-
- IF (LeftLength + RightLength) > (HIGH (Result) + 1) THEN
-
- (*--- The length of the resulting string would exceed ---*)
- (*--- the length available in Result. ---*)
-
- RETURN ( FALSE );
-
- ELSE
-
- (*--- Copy the contents of LeftString into Result. ---*)
-
- Assign ( LeftString, Result );
-
- (*--- Copy each character of RightString into Result, ---*)
- (*--- starting at the end. ---*)
-
- ResultIndex := LeftLength;
- IF RightLength > 0 THEN
- FOR SourceIndex := 0 TO RightLength - 1 DO
- Result[ResultIndex] := RightString[SourceIndex];
- INC ( ResultIndex );
- END;
- END;
-
- IF ResultIndex <= HIGH (Result) THEN
-
- (*--- Result can contain more characters than that ---*)
- (*--- obtained from concatenating LeftString with ---*)
- (*--- RightString, so terminate Result with a null ---*)
- (*--- character. ---*)
-
- Result[ResultIndex] := CHR (0);
- END;
- RETURN ( TRUE );
- END;
- END ConcatString;
-
- (********************************************************************)
-
- PROCEDURE UpperCase ( VAR String : (* IN *) ARRAY OF CHAR );
-
- VAR
- Index : CARDINAL;
- Ch : CHAR;
-
- BEGIN
- FOR Index := 0 TO HIGH ( String ) DO
- Ch := String[Index];
- IF (Ch >= 'a') AND (Ch <= 'z') THEN
- String[Index] := CHR ( ORD (Ch) - (ORD ('a') - ORD ('A')));
- END;
- END (* DO *);
- END UpperCase;
-
- END Text.
-
-